package com.goldsprite.infinityworld.screens.examples.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;
import com.badlogic.gdx.scenes.scene2d.ui.Touchpad.TouchpadStyle;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.goldsprite.gdxcore.screens.GScreen;
import org.w3c.dom.Text;
import com.goldsprite.infinityworld.assets.GlobalAssets;

public class TouchpadExampleScreen extends GScreen {
	private Stage stage;
	private Touchpad touchpad;
	private float playerX = 200;
	private float playerY = 200;
	private float speed = 10f;
	private Texture playerTex;
	private TextureRegion playerRegion;

	@Override
	public void create() {
		stage = new Stage(getViewport());
		getImp().addProcessor(stage);

		playerTex = new Texture(Gdx.files.internal("roles/Orc.png"));
		playerRegion = new TextureRegion(playerTex);
		playerRegion.setRegion(0, 0, 100, 100);

		// 创建皮肤
		Skin skin = new Skin(Gdx.files.internal(GlobalAssets.skinPath));
		
		// 创建摇杆
		touchpad = new Touchpad(5, skin); // 10 是死区半径，摇杆在此范围内不触发动作
		touchpad.setBounds(15, 15, 150, 150); // 设置位置和大小
		stage.addActor(touchpad);
	}

	@Override
	public void render(float delta) {
		super.render(delta);
		//ScreenUtils.clear(Color.GRAY);

		// 获取摇杆输入
		float deltaX = touchpad.getKnobPercentX(); // X方向输入（-1到1）
		float deltaY = touchpad.getKnobPercentY(); // Y方向输入（-1到1）

		// 更新玩家位置
		playerX += deltaX * speed;
		playerY += deltaY * speed;

		// 限制玩家位置不超出屏幕边界
		playerX = Math.max(0, Math.min(getViewSize().x - 50, playerX));
		playerY = Math.max(0, Math.min(getViewSize().y - 50, playerY));

		// 绘制玩家和摇杆
		stage.act(delta);
		stage.draw();

		// 绘制玩家方块
		stage.getBatch().begin();
		//stage.getBatch().setColor(Color.BLUE);
		stage.getBatch().draw(playerRegion, playerX-500/2f, playerY-500/2f, 500, 500); // 使用一个玩家纹理
		stage.getBatch().end();
	}

	@Override
	public void dispose() {
		stage.dispose();
	}
}

